﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Department_JobDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
        //    if (Request["action"] != null)
        //    {
        //        Bind(Request["action"].ToString());
        //        DptForm.Visible = true;
        //    }
        //    else
        //    {

        //        DptForm.Visible = false;
        //    }
        //    DepartmentTreeView.Bind(DepartmentTreeView.DptAndJob);

        //}
         
        if (!IsPostBack)
        {
            if (Request["action"].ToString() == "add")
            {
                AddBind();
            }
            else
            {
                Bind(Request["jid"].ToString());
            }
        }
    }

    private void AddBind()
    {
        Save.CommandArgument = "0," + SaveMode.AddChild;

        Del.Enabled = false;

        Del.SelectedValue = "0";

        lblMode.Text = "职位添加";
    }

    private void Bind(string Jid)
    {
        object[] obj = new object[] { Jid };


        object[] ret = Srvtools.CliUtils.CallMethod("SDepartment", "GetCompanyJobDetail", obj);

        DataTable dt = ((DataSet)ret[1]).Tables[0];

        if (dt.Rows.Count <= 0)
        {
            return;
        }

        DptForm.Visible = true;

        Cancel.Enabled = false;

        Add.Enabled = true;


        JobName.Text = dt.Rows[0]["JobName"].ToString();

        JobContent.Text = dt.Rows[0]["JobContent"].ToString();

        Jobfunction.Text = dt.Rows[0]["Jobfunction"].ToString();

        Jobpower.Text = dt.Rows[0]["Jobpower"].ToString();

        WorkPermit.Text = dt.Rows[0]["WorkPermit"].ToString();

        dpt.ddlSelectValue(dt.Rows[0]["ParDepartmentId"].ToString());

        Del.SelectedValue = "0";

        Save.CommandArgument = dt.Rows[0]["JobId"].ToString() + "," + SaveMode.Save;

        lblMode.Text = "职位变更";
    }

    private enum SaveMode : int
    {
        Save,
        AddChild

    }

    protected void Add_Click(object sender, EventArgs e)
    {
        Add.Enabled = false;

        Del.Enabled = false;

        Cancel.Enabled = true;




        Save.CommandArgument = "0," + SaveMode.AddChild;

        JobName.Text = "";

        JobContent.Text = "";

        Jobfunction.Text = "";

        Jobpower.Text = "";

        WorkPermit.Text = "";

        Del.SelectedValue = "0";


        lblMode.Text = "职位添加";
    }

    protected void Cancel_Click(object sender, EventArgs e)
    {
        Cancel.Enabled = false;
        Add.Enabled = true;
        Del.Enabled = true;
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string[] SaveInfo = Save.CommandArgument.ToString().Split(',');

        if (SaveInfo.Length <= 0)
        {
            return;
        }

        if (SaveInfo[1] == SaveMode.Save.ToString())
        {
            Change(SaveInfo[0].ToString());
        }
        else if (SaveInfo[1] == SaveMode.AddChild.ToString())
        {
            AddChild();
        }
    }

    private void Change(string Jid)
    {
        try
        {
            object[] ret = null;
            object[] obj = null;
            if (Del.SelectedValue == "1")
            {
                //GetEmpExistJob
                ret = Srvtools.CliUtils.CallMethod("SDepartment", "GetEmpExistJob", new object[] { Jid });
                if (Convert.ToInt32(ret[1]) <= 0)
                {
                    obj = new object[] { Jid, JobName.Text, dpt.SelectDepmanetId, Del.SelectedValue, JobContent.Text, Jobfunction.Text, Jobpower.Text, WorkPermit.Text };
                    ret = Srvtools.CliUtils.CallMethod("SDepartment", "UpdateCompanyJob", obj);
                }
                else
                {
                    throw new Exception("职位作废失败！作废职位下不得存在未离职员工");
                }
            }
            else
            {
                obj = new object[] { Jid, JobName.Text, dpt.SelectDepmanetId, Del.SelectedValue, JobContent.Text, Jobfunction.Text, Jobpower.Text, WorkPermit.Text };
                ret = Srvtools.CliUtils.CallMethod("SDepartment", "UpdateCompanyJob", obj);
            }

            if ((bool)ret[1])
            {
                ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('职位变更成功');parent.location.href='JobSet.aspx'", true);
            }
            else
            {
               
                throw new Exception("职位变更失败");
            }
        }
        catch(Exception ex)
        {
            Del.SelectedValue = "0";
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('" + ex.Message+ "')", true);
        }
    }

    private void AddChild()
    {
        object[] obj = new object[] { "0", JobName.Text, dpt.SelectDepmanetId, "0", JobContent.Text, Jobfunction.Text, Jobpower.Text, WorkPermit.Text };


        object[] ret = Srvtools.CliUtils.CallMethod("SDepartment", "InsertCompanyJob", obj);


        if ((bool)ret[1])
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('职位添加成功');parent.location.href='JobSet.aspx'", true);
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('职位添加失败');parent.location.href='JobSet.aspx'", true);
        }

    }
}
